﻿@model Controller2.Models.Pessoa_PJ
@{
    Layout = null;
    var newFornec = Model.IdPessoa > 0 ? false : true;
}
<!DOCTYPE html>
<html class="no-js">
<head>
    <title>Cadastro</title>
    @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))
    @if (false)
    {
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/Content/themes/controller/controller.css")" rel="stylesheet" type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>    
        <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    }
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/controller.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/numbers.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <style type="text/css">
        .display-label, .editor-label
        {
            width: 130px;
        }
        #IdEstado, #IdCidade
        {
            width: 400px;
        }
        #IdEstado
        {
            border: 1px solid #ff0000;
        }
        input[type=text]#CEP
        {
            width: 80px;
        }
        input[type=text]#Fax, input[type=text]#Fone
        {
            width: 125px;
        }
        input[type=text]#Contato, input[type=text]#CNPJ, input[type=text]#InscricaoEstadual
        {
            width: 200px;
        }
        textarea#EmailContato, textarea#NomePessoa, textarea#Endereco, textarea#Bairro, textarea#Site, textarea#Email, textarea#Twitter, textarea#Complemento, textarea#UserName
        {
            width: 400px;
        }
    </style>
    @if (false)
    {
        <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui.js" type="text/javascript"></script>
        <script src="../../Scripts/controller.js" type="text/javascript"></script>
    }
</head>
<body id="frame">
    <div id="formHide">
        @using (Html.BeginForm("Salvar", "Fornecedor"))
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>Fornecedor</legend>
                @Html.HiddenFor(model => model.IdPessoa)
                <div class="editor-label">
                    @Html.LabelFor(model => model.DataCadastro, "Data de Cadastro: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.DataCadastro)
                    @Html.ValidationMessageFor(model => model.DataCadastro)
                    &nbsp;
                    @Html.LabelFor(model => model.DataAbertura, "Data Abertura: ")
                    @Html.EditorFor(model => model.DataAbertura)
                    @Html.ValidationMessageFor(model => model.DataAbertura)
                    @if (Model.IdPessoa > 0)
					{
						@Html.CheckBoxFor(model => model.Fornecedor, new { style = "margin-left: 25px;", disabled = "disabled" })
						<span id="fornec">
							@Html.LabelFor(model => model.Fornecedor, "É Cliente?")
						</span>
					}
					else
					{
						@Html.CheckBoxFor(model => model.Fornecedor, new { style = "margin-left: 25px;" })
						<span id="fornec">
							@Html.LabelFor(model => model.Fornecedor, "É Cliente?")
						</span>
					}
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.NomePessoa, "Nome: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.NomePessoa)
                    @Html.ValidationMessageFor(model => model.NomePessoa)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.InscricaoEstadual, "Insc. Estadual: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.InscricaoEstadual)
                    @Html.ValidationMessageFor(model => model.InscricaoEstadual)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.CNPJ, "CNPJ: ")
                    @Html.EditorFor(model => model.CNPJ)
                    @Html.ValidationMessageFor(model => model.CNPJ)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Endereco, "Endereço: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Endereco)
                    @Html.ValidationMessageFor(model => model.Endereco)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.NumEnd, "Nº: ")
                    @Html.EditorFor(model => model.NumEnd)
                    @Html.ValidationMessageFor(model => model.NumEnd)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Complemento, "Complemento: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Complemento)
                    @Html.ValidationMessageFor(model => model.Complemento)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Bairro, "Bairro: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Bairro)
                    @Html.ValidationMessageFor(model => model.Bairro)
                    @Html.LabelFor(model => model.CEP, "CEP: ")
                    @Html.EditorFor(model => model.CEP)
                    @Html.ValidationMessageFor(model => model.CEP)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdEstado, "Estado: ")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.IdEstado, (IEnumerable<SelectListItem>)ViewBag.Estado, "", new { style = "width: 200px;" })
                    @Html.ValidationMessageFor(model => model.IdEstado)
                    &nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.IdCidade, "Cidade: ")
                    @if (Model.IdCidade > 0)
                    {
                        @Html.DropDownListFor(model => model.IdCidade, (IEnumerable<SelectListItem>)ViewBag.Cidade, "", new { style = "width: 252px;" })
                    }
                    else
                    {
                        @Html.DropDownListFor(model => model.IdCidade, (IEnumerable<SelectListItem>)ViewBag.Cidade, "", new { style = "width: 252px;", disabled = "disabled" })
                    }
                    @Html.ValidationMessageFor(model => model.IdCidade)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Fone, "Fone: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Fone)
                    @Html.ValidationMessageFor(model => model.Fone)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.Fax, "Fax: ")
                    @Html.EditorFor(model => model.Fax)
                    @Html.ValidationMessageFor(model => model.Fax)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Site, "Site: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Site)
                    @Html.ValidationMessageFor(model => model.Site)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Email, "E-mail: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Email)
                    @Html.ValidationMessageFor(model => model.Email)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Twitter, "Twitter: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Twitter)
                    @Html.ValidationMessageFor(model => model.Twitter)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Contato, "Nome Contato: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Contato)
                    @Html.ValidationMessageFor(model => model.Contato)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.RamalContato, "Ramal: ")
                    @Html.EditorFor(model => model.RamalContato)
                    @Html.ValidationMessageFor(model => model.RamalContato)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.EmailContato, "Email Contato: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.EmailContato)
                    @Html.ValidationMessageFor(model => model.EmailContato)
                </div>
            </fieldset>                 
            <div id="botoesForm">
                <input type="submit" id="Gravar" value="Gravar" />
                <input type="button" id="Fechar" value="Cancelar" />
            </div>
        }
    </div>
    <div id="alertaErro" style="display: none">
    </div>
    <script type="text/javascript">
        var novo = ('@newFornec'.toLowerCase() === 'true');
        var cnpjAntigo = '@Model.CNPJ';
        $.validator.methods.existeCnpj = function (value, element, param) {
            showLoading();
            var ok = true;
            if (cnpjAntigo != $(element).val()) {
                $.ajax({
                    url: '@Url.Action("ExisteCNPJ", "ValidacoesRemotas")',
                    type: 'GET',
                    async: false,
                    data: { cnpj: $(element).val() },
                    error: function (x, e, thrownError) {
                        hideLoading();
                        alertError('Erro ' + x + ': ' + thrownError);
                        ok = false;
                    },
                    success: function (data) {
                        hideLoading();
                        if (data.existe) {
                            alertError(data.error);
                            ok = false;
                        } else {
                            ok = true;
                        }
                    }
                });
            }
            return ok;
        };
        $(function () {
            $('#formHide').fadeIn('slow');
            $('#IdEstado').rules('add', 'required');
            if (novo) {
                $('#DataNascimento').empty();
            }
            $('.datepicker').mask("99/99/9999");
            $("#Fone").mask("(99) 9999-9999");
            $("#Celular").mask("(99) 9999-9999");
            $("#CNPJ").mask("99.999.999/9999-99");
            $("#CNPJ").rules('add', { existeCnpj: true, messages: { existeCnpj: "*"} });
            $("#CEP").mask("99999-999");
            hideLoading();
            if (novo) {
                $('#DataAbertura').focus();
            } else {
                $('#NomePessoa').focus();
            }
        });
        $('#Fechar').click(function () { closeWindow(); });
        $('form').submit(function (e) {
            e.preventDefault();
            var datavalida = true;
            $('.datepicker').each(function () {
                if (!validaData(this)) {
                    datavalida = false;
                    $(this).focus();
                }
            });
            if (datavalida) {
                if ($('form').valid()) {
                    $.post('@Url.Action("Salvar", "Fornecedor")', $('form').serialize(), function (data) {
                        if (data.salvo) {
                            closeWindow();
                        } else {
                            alertError(data.error);
                        }
                        $('#gravar').attr('disabled', false);
                        hideLoading();
                    });                    
                }
            }
        });
        $('#IdEstado').change(function (e) {
            showLoading();
            var idEstado = $('#IdEstado option:selected').val();
            if (idEstado == "") {
                $('#IdCidade').empty();
                $('#IdCidade').attr('disabled', 'disabled');
            } else {
                $('#IdCidade').empty();
                $('#IdCidade').append('<option>Carregando...</option>');
                var url = '@Url.Action("CarregarComboCidade", "Cliente")';
                $.post(url, { idEstado: idEstado }, function (dados) {
                    $('#IdCidade').empty();
                    $.each(dados, function (index, cidade) {
                        $('#IdCidade').append('<option value="' + cidade.Value + '">' + cidade.Text + '</option>');
                    });
                    $('#IdCidade').removeAttr('disabled').focus();
                });
            }
            hideLoading();
        });
        $('.datepicker').blur(function () {
            if (!validaData(this)) {
                $(this).focus();
            }
        });
    </script>
</body>
</html>
